package com.xiaoshuidi.cloud.module.member.controller.app.coupon;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.security.core.annotations.PreAuthenticated;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.api.coupon.vo.CouponCheckReqVo;
import com.xiaoshuidi.cloud.module.member.api.coupon.vo.CouponLockVo;
import com.xiaoshuidi.cloud.module.member.api.coupon.vo.CouponReturn;
import com.xiaoshuidi.cloud.module.member.controller.app.coupon.vo.*;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponCustomerService;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ivan.Wu
 * @description 优惠券
 * @date 2023/3/21 10:34
 */
@Tag(name = "租客APP - 优惠券")
@RequestMapping(ApiConstants.NAME + "/coupon")
@RestController
public class AppCouponController {
    @Autowired
    private CouponCustomerService couponCustomerService;
    @Autowired
    private CouponService couponService;

    /**
     * 我的优惠券
     */
    @GetMapping("/coupons")
    @Operation(summary = "我的优惠券")
    @PreAuthenticated
    public CommonResult<List<CouponCustomerVo>> myCoupon(@RequestParam(value = "usageStatus") Integer usageStatus) {
        Long tenantUserId = SecurityFrameworkUtils.getLoginUserId();
        List<CouponCustomerVo> list = couponCustomerService.couponCustomer(tenantUserId, usageStatus);
        return CommonResult.success(list);
    }

    @GetMapping("/available")
    @Operation(summary = "查询可用优惠券")
    @PreAuthenticated
    public CommonResult<List<CouponCustomerValidQueryVo>> queryAbledCoupon() {
        Long tenantUserId = SecurityFrameworkUtils.getLoginUserId();
        List<CouponCustomerValidQueryVo> couponVos = couponCustomerService.queryAbledCoupon(tenantUserId);
        return CommonResult.success(couponVos);
    }

    @GetMapping("/unavailable")
    @Operation(summary = "查询不可用优惠券")
    @PreAuthenticated
    public CommonResult<List<CouponCustomerValidQueryVo>> queryDisabledCoupon() {
        Long tenantUserId = SecurityFrameworkUtils.getLoginUserId();
        List<CouponCustomerValidQueryVo> couponVos = couponCustomerService.queryDisabledCoupon(tenantUserId);
        return CommonResult.success(couponVos);
    }


    @PostMapping("/check-bill-coupons")
    @Operation(summary = "优惠券使用校验")
    CommonResult<List<CouponSimpleInfoVo>> checkBillCoupons(@RequestBody CouponCheckReqVo reqVo) {
        return CommonResult.success(couponCustomerService.checkBillCoupons(reqVo));
    }


    @PostMapping("/check-bill-coupons_overlay")
    @Operation(summary = "优惠券叠加使用校验")
    CommonResult<Boolean> checkBillCouponsOverlay(@RequestBody CouponCheckReqVo reqVo) {
        return CommonResult.success(couponCustomerService.checkBillCouponsOverlay(reqVo));
    }

    @PostMapping("/coupons-return")
    @Operation(summary = "优惠券优惠信息返回")
    CommonResult<CouponReturn> couponReturn(@RequestBody CouponLockVo couponLockVo){
        CouponReturn couponReturn = couponCustomerService.couponReturn(couponLockVo);
        return CommonResult.success(couponReturn);
    }
}
